package com.slaffka.security;

import javax.enterprise.context.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.slaffka.model.RoleType;
import org.jboss.seam.security.Identity;

import java.io.IOException;
import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: slaffka
 * Date: 04.11.12
 * Time: 13:30
 * To change this template use File | Settings | File Templates.
 */
@Named("securityController")
@RequestScoped
public class SecurityController implements Serializable {

    @Inject
    Identity identity;

    public void logout() throws IOException {
        //identity.logout();
//        identity.removeRole(RoleType.ADMIN.getRoleTypeStr(), "default", "default");
//        final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
////        System.out.println("externalContext.getContextName() = " + externalContext.getContextName() + ", " + externalContext.getContext());
////        final HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
//        final HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
////        System.out.println("/getContextPath = " + request.getContextPath() + ", request.getPathInfo() = " + request.getPathInfo());
//        response.sendRedirect("/login.jsf");
//        externalContext.invalidateSession();

        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        externalContext.invalidateSession();
        externalContext.redirect("/login");
    }
}
